# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, pysozluk-qt Development Team
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#

from PyQt4 import QtGui
from database import pysozlukDatabase
from config import pysozlukConfiguration
from gettext import translation
import pysozlukglobals
from os import getenv

_ = translation('pysozluk-qt', fallback=True).ugettext

class pysozlukCore:
    def __init__(self, ui, mainWindow):
        self.ui = ui
        self.mw = mainWindow
        self.config = pysozlukConfiguration()
        self.database = pysozlukDatabase()

        if self.config.general.offline:
            self.ui.actionOffline.setChecked(True)
        else:
            self.ui.actionOffline.setChecked(False)

        #TODO: Find a way to set default button, search should be called with pressing enter

    def search(self):
        #TODO: This should display country flags
        self.ui.textBrowser.clearHistory()
        keyword = unicode(self.ui.lineEdit.text())
        translation = self.database.search(keyword)
        turkish = []
        english = []
        tr = "<b>" + _("Turkish translation of %s") % keyword + "</b><br>"
        en = "<b>" + _("English translation of %s") % keyword + "</b><br>"
        result = ""

        if translation.__len__()<>0:
            for var in translation:
                if var.away:
                    turkish.append(var.text)
                if var.home:
                    english.append(var.text)
        
            if turkish.__len__()<>0:
                for text in turkish:
                    tr += "%d. %s<br>" % (turkish.index(text)+1, text)
                tr += "<br>"
                result += tr

            if english.__len__()<>0:
                for text in english:
                    en += "%d. %s<br>" % (english.index(text)+1, text)
                result += en
        else:
            result = "<b>" + _("Cannot find \"%s\"") % keyword + "</b>"

        self.ui.textBrowser.setHtml(result.replace('"',''))

    def about(self):
        QtGui.QMessageBox.about(self.mw,
                                _("About Pysozluk-Qt"),
                                "Pysozluk-Qt v%s\n" % pysozlukglobals.version +\
                                "http://code.google.com/p/pysozluk-qt\n\n" +\
                                _("Developers:") + "\n" +\
                                u"İlker Kesen <ilker.kde at gmail.com>" + "\n" +\
                                u"Uğur Çetin <ugur.jnmbk at gmail.com>" + "\n" )

    def aboutQt(self):
        QtGui.QMessageBox.aboutQt(self.mw)

    def toggleOffline(self):
        if self.ui.actionOffline.isChecked():
            self.config.general.offline = True
        else:
            self.config.general.offline = False
        self.config.write()

    def save(self):
        if self.ui.textBrowser.toPlainText() == "":
            QtGui.QMessageBox.warning(self.mw, _("Warning"), _("If you want to save a description, you must search a keyword."))
        else:
            #TODO: Add HTML and Plain Text file filters
            file_path = QtGui.QFileDialog.getSaveFileName(self.mw, _("Save"), getenv("HOME"), "All Files (*)")
            if file_path.__str__().endswith(".html"):
                description = self.ui.textBrowser.toHtml()
            else:
                description = self.ui.textBrowser.toPlainText()

            file = open(file_path.__str__(), "w")
            file.write(description + "\n")
            file.close()
